package com.duoqin.common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Log;


public class NetworkUtils {
     public static boolean isConnected(Context context){
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        if (cm == null) return false;
        NetworkInfo info = cm.getActiveNetworkInfo();
        return (info != null && info.isConnected());
    }

    public static boolean isWiFi4GNetwork(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (ConnectivityManager.TYPE_WIFI == networkInfo.getType()) {
                Log.d("duoqin", "isWiFi4GNetwork networkType is wifi");
                return true;
            } else if (ConnectivityManager.TYPE_MOBILE == networkInfo.getType()) {
                String subTypeName = networkInfo.getSubtypeName();
                int networkType = networkInfo.getSubtype();
                Log.d("duoqin", "isWiFi4GNetwork networkType is " + networkType
                        + "; subTypeName = " + subTypeName);
                return TelephonyManager.NETWORK_TYPE_LTE == networkType;
            }
        }
        return false;
    }
}
